package com.hhsj.dynamicui.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class AdJDBCService {

    private Connection mConnection;

    private boolean initialized = false;

    private AdJDBCService() {

    }

    public void init(String ip, String port, String user, String pwd) {
        try {
            //"jdbc:mysql://192.168.1.11:3306/demo?serverTimezone=UTC"
            String url = String.format("jdbc:mysql://%s:%s/business?serverTimezone=UTC", ip, port);
            System.out.println(url + " " + user + " " + pwd);
            mConnection = DriverManager.getConnection(url, user, pwd);
            initialized = true;
            System.out.println("已完成初始化");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    enum IN {
        INSTANCE;

        AdJDBCService mConnectService;

        IN() {
            mConnectService = new AdJDBCService();
        }

        public AdJDBCService getService() {
            return INSTANCE.mConnectService;
        }
    }

    public static AdJDBCService getInstance() {
        return IN.INSTANCE.getService();
    }

    public Connection getConnection() {
        if (!initialized) {
            throw new IllegalStateException("JDBC connection has not initialized");
        }
        return mConnection;
    }
}
